package com.qqtop10.manager.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.KeyEntity;
import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.Rebate;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.GoodsService;
import com.qqtop10.manager.service.RebateService;

/**
 * 
 * @ClassName: RebateController
 * @Description: 返利链接Controller
 * 
 */
@RequestMapping("/manager/rebate/")
@Controller
public class RebateController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(RebateController.class);
	@Autowired
	private RebateService rebateService;
	
	@Autowired
	private GoodsService goodsService;
	@RequestMapping("pagelist.do")
	@ResponseBody
	public Result pagelist(Rebate input) {
		try {
			PagingArgs pagingArgs = getPagingArgs();
			int count = rebateService.count(input);
			List<Rebate> array = rebateService.all(input, pagingArgs);
			return page(count, array);
		} catch (Exception e) {
			return excep(e);
		}
	}
	
	@RequestMapping("updateRebate.do")
	@ResponseBody
	public Result UpdateRebate(Rebate input) {
		String value = getParameter(input.getRemark());
		input.setRemark(value);
		input.setUpdatetime(now());
		input.setMender(getLogindUuid());
		int ret = rebateService.update(input);
		if (ret > 0) {
			return ok("更新成功");
		} else {
			return err("操作失败");
		}
	}

	@RequestMapping("deleteById.do")
	@ResponseBody
	public Result DeleteById(int ids) {
		int ret = rebateService.delete(ids);
		if (ret > 0) {
			return ok("删除成功");
		} else {
			return err("操作失败");
		}
	}

	@RequestMapping("approve.do")
	@ResponseBody
	public Result approve(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String[] uuids = request.getParameterValues("uuids[]");
		int i = 0;
		try {
			i = rebateService.approve(Arrays.asList(uuids));
			if (i == uuids.length) {
				KeyEntity input = new KeyEntity();
				input.setUuid(uuids[0]);
				Rebate rebate = rebateService.selectById(input);
				if (uuids[0] != null) {
					ModelMap map = new ModelMap();
					map.put("rebate_amt", rebate.getRebate_amt());
					map.put("rebate_coupon", rebate.getRebate_coupon());
					map.put("ruser_uuid", rebate.getRuser_uuid());
					rebateService.addCoupon(map);
				}
				if (rebate.getPuser_uuid() != null) {
					ModelMap map = new ModelMap();
					map.put("rebate_amt", rebate.getRebate_samt());
					map.put("rebate_coupon", rebate.getRebate_scoupon());
					map.put("ruser_uuid", rebate.getPuser_uuid());
					rebateService.addCoupon(map);
				}
				if (rebate.getTuser_uuid() != null) {
					ModelMap map = new ModelMap();
					map.put("rebate_amt", rebate.getRebate_tamt());
					map.put("rebate_coupon", rebate.getRebate_tcoupon());
					map.put("ruser_uuid", rebate.getTuser_uuid());
					rebateService.addCoupon(map);
				}
				return ok("审核成功！");
			} else {
				return err("审核失败：参数错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("审核失败：" + e.getMessage());
			return err("审核失败");
		}
	}
}
